home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 346 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: gate.net!pslfl2-18
  2. From: bhutto@gate.net (William Hutto)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: my atoi function, could someone suggest...
  5. Date: 4 Jan 1996 16:14:53 GMT
  6. Organization: CyberGate, Inc.
  7. Message-ID: <4cgudt$1e9i@news.gate.net>
  8. References: <4cf7ap$q4u@kaleka.seanet.com>
  9. NNTP-Posting-Host: pslfl2-18.gate.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <4cf7ap$q4u@kaleka.seanet.com>,
  13.    mitchell@seanet.com (where am i?) wrote:
  14. >Hello.  This is my first attempt at an atoi function.  Although it
  15. >works and behaves just like atoi, I was wondering if anyone would care
  16. >for a look and make suggestions back to me about its efficiency.
  17. >
  18. >int atoi ( char *change )
  19. >{
  20. >    int newint = 0, sign = 1;
  21. >
  22. >    while ( *change )
  23. >    {
  24. >        if ( (*change < '0' || *change > '9') && *change != '-' )
  25.  
  26.         if (!isdigit(*change) && *change != '-')
  27.  
  28. If I were to assume you didn't want to use library functions or macros in this 
  29. function, I wouldn't say anything about this line. Other than that, you could 
  30. use isdigit() (#include <ctype.h>) which can use a lookup table and should be 
  31. faster. 
  32.  
  33. >        {
  34. >            *change++;
  35.  
  36.             change++;
  37.  
  38. You don't need indirection here.
  39.  
  40. >        }
  41. >        else
  42. >        {
  43. >            if ( *change == '-' )
  44. >            {
  45. >                *change++;
  46.  
  47.                 change++;
  48.  
  49. >                if ( *change >= '0' && *change <= '9' )
  50.  
  51.                 if(isdigit(*change))
  52.  
  53. >                    sign = -1;
  54. >            }
  55. >            if ( *change >= '0' && *change <= '9' )
  56.  
  57.             if(isdigit(*change))
  58.  
  59. >            {
  60. >                while ( sign )
  61. >                {
  62. >                    newint *= 10;
  63. >                    newint = newint + *change - 48;
  64.  
  65.                     newint = newint + *change - '0';
  66.  
  67. Just in case '0' != 48.
  68.  
  69. >                    *change++;
  70.  
  71.                     change++;
  72.  
  73. >                    if ( *change < '0' || *change > '9' )
  74.  
  75.                     if (!isdigit(*change))
  76.  
  77. >                    {
  78. >                        return (  newint * sign );
  79. >                        exit (0);
  80.  
  81. exit() should be omitted.
  82.  
  83. >                    }
  84. >                }
  85. >            }
  86. >        }
  87. >    }
  88. >    return ( 0 );
  89. >}
  90. >
  91. >Thank you all very much.
  92. >
  93. >Mitch
  94. >
  95.  
  96.  
  97. "Whatcha got on?...Your mind?"
  98.